package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.cache.GroupNameIDCache;
import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.cache.URILabelCache;
import cn.edu.nju.ws.sview.cache.UserNameIDCache;
import cn.edu.nju.ws.sview.rdf.URIResource;
import cn.edu.nju.ws.sview.util.URIUtil;
import cn.edu.nju.ws.sview.views.Collaboration;
import cn.edu.nju.ws.sview.views.Group;
import cn.edu.nju.ws.sview.views.data.EntityBrowsing;
import cn.edu.nju.ws.sview.views.data.EntityManagement.URIMember;
import cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset;
import cn.edu.nju.ws.sview.wyge.relations.adataset.DocumentDataset;
import cn.edu.nju.ws.sview.wyge.relations.adataset.bookmark.DatasetBookmark;
import cn.edu.nju.ws.sview.wyge.relations.adataset.comment.DatasetComment;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetBrowsing;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetBrowsing.RecomdDatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.index.SearchIndexManager;

/**
 * Servlet implementation class Entity
 */
@WebServlet("/dataset")
public class Dataset extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String STATUS_CONFLICT = "conflict";
	public static final String STATUS_OK = "ok";
	public static final String STATUS_REMOVE = "removed";
	public static final String STATUS_NOAUTHORITY="noauthority";
	public static final String STATUS_MEMBER_CONFLICT="memconflict";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Dataset() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String option = null;
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("info") != null) {//for index.jsp popular or favourite(bookmark)
			json = request.getParameter("info");
			option = "info";
		} else if (request.getParameter("hint") != null) {//for index.jsp autocomplete
			json = request.getParameter("hint");
			option = "hint";
		} else if (request.getParameter("query") != null) {
			json = request.getParameter("query");
			option = "query";
		} else if (request.getParameter("mgdataset") != null){
			json = request.getParameter("mgdataset");
			option = "mgdataset";
		} else if (request.getParameter("gmem") != null){
			json = request.getParameter("gmem");
			option = "gmem";
//		} else if (request.getParameter("checkname") != null){
//			json = request.getParameter("checkname");
//			option = "checkname";
		} else if (request.getParameter("checkmemURI") != null){
			json = request.getParameter("checkmemURI");
			option = "checkmemURI";
		} else if (request.getParameter("recommendEntity") != null) {
			json = request.getParameter("recommendEntity");
			option = "recommendEntity";
		} else if (request.getParameter("svdataset") != null){
			json = request.getParameter("svdataset");
			option = "svdataset";
		} else if (request.getParameter("hasRDFDesc") != null){
			json = request.getParameter("hasRDFDesc");
			option = "hasRDFDesc";
		}else if (request.getParameter("indexSearchDataset") != null){
			json = request.getParameter("indexSearchDataset");
			option = "indexSearchDataset";
//		} else if (request.getParameter("mergeEntity") != null) {
//			json = request.getParameter("mergeEntity");
//			option = "mergeEntity";
		} else{
			System.out.print("error! Dataset: request:"+request.getParameterNames().toString());
		}
		
		if (option.equals("info")) {
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String type = (String) regtJSON.get("type");
			String lang = (String) regtJSON.get("lang");
			int start = ((Long) regtJSON.get("start")).intValue();
			int length = ((Long) regtJSON.get("length")).intValue();
			ArrayList<DatasetItem> array = null;
			if (type.equals("popAll")) {//for popular
				array = DatasetBrowsing.fetchAll(start, length, lang);
			} else if (type.equals("mineAll")) {//for favourite(bookmark)
				System.out.println("error! called by index.js");
				String user = (String) regtJSON.get("user");
				array = DatasetBrowsing.fetchUserAll(user, start, length);//, lang);
			}
			JSONArray elements = new JSONArray();
			for (DatasetItem item : array) {
				JSONObject jsob = new JSONObject();
				jsob.put("genre", item.getGenre());
				jsob.put("id", item.getId());
//				jsob.put("name", item.getName());
//				System.out.println(item.toString()+"|name:"+DatasetItem.getShowNameByDatasetID(item, lang));
//				if(item.getId() == 10851336){
//					System.out.println("d:"+item.toString()+"|id:"+DatasetItem.getShowNameByDatasetID(item, lang));
//				}
				String name = DatasetItem.getShowNameByDatasetID(item, lang);
				if(name==null || name.trim().length()<=0){
					System.out.println("delete item:"+item.toString());
					DatasetBrowsing.deleteVisit(item);
					continue;
				}
				jsob.put("name", name);//item.getLabelForShow(lang));//====
//				if(item.getGenre().contentEquals("uri")){
				if(item.getGenre().contentEquals(DatasetItem.URI_GENRE)){
					jsob.put("uri", item.getUri());
//					try {
//						jsob.put("uri", URIIDCache.getInstance().getURI(item.getId()));
//					} catch (Throwable e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
				}
				elements.add(jsob);
			}
			JSONObject result = new JSONObject();
			result.put("result", elements);
			response.getWriter().print(result);
		} else if (option.equals("hint")) {
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String kw = (String) regtJSON.get("kw");
			String lang = (String) regtJSON.get("lang");
			ArrayList<DatasetItem> array = null;
			array = DatasetBrowsing.kw2Datasets(kw, lang);

			JSONArray elements = new JSONArray();
			for (DatasetItem item : array) {
				JSONObject jsob = new JSONObject();
				jsob.put("genre", item.getGenre());
				jsob.put("id", item.getId());
//				jsob.put("name", item.getName());
				jsob.put("name", item.getShowName());//DatasetItem.getShowNameByDatasetID(item, lang));//item.getLabelForShow(lang));
				elements.add(jsob);
			}
			JSONObject result = new JSONObject();
			result.put("result", elements);
			response.getWriter().print(result);
		} else if (option.equals("query")) {
//			System.out.println("in Dataset.query===check name");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String kw = URLDecoder.decode((String) regtJSON.get("kw"), "UTF-8").trim();
			String lang = (String) regtJSON.get("lang");
			DatasetItem ds = DatasetBrowsing.uri2Dataset(kw, lang);
			if (ds != null) {
				JSONObject jsob = new JSONObject();
				jsob.put("genre", ds.getGenre());
				jsob.put("id", ds.getId());
//				jsob.put("name", ds.getName());
				jsob.put("name", ds.getShowName());//ds.getLabelForShow(lang));
				JSONObject result = new JSONObject();
				result.put("result", jsob);
				response.getWriter().print(result);
			} else {
				JSONObject result = new JSONObject();
				result.put("result", "");
				response.getWriter().print(result);
			}
		} else if (option.equals("mgdataset")){
			String str = URLDecoder.decode(json, "utf8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
//			String ds_type = (String)regtJSON.get("etype");
//			System.out.println("mgdataset   ds_type:"+ds_type);
//			int datasetId = Integer.parseInt((String) regtJSON.get("eid"));
//			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
			String ugname = (String)request.getSession().getAttribute("groupName");
			if(ugname == null){
				System.out.println("in mgdataset, no groupName session!");
				JSONObject jsob = new JSONObject();
				jsob.put("err", "no gsession");
				response.getWriter().print(jsob);
				return;				
			}
			
			int ugid = Group.getGroupIdByName(ugname);
			String lang = (String) regtJSON.get("lang");
//			String gname = null;
//			try {
//				gname = GroupNameIDCache.getInstance().getName(groupID);
//			} catch (Throwable e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			int start = 0;//===
			int length = 20;//===
			ArrayList<DatasetItem> set = DatasetBrowsing.fetchGroupAll(ugid,start, length);//, lang);//(groupID, start, length, lang);
			JSONArray result = new JSONArray();
			for(DatasetItem item : set){
				String item_type = item.getGenre();
				int item_id = item.getId();
//				String item_name = item.getName();
				String item_name = DatasetItem.getShowNameByDatasetID(item, lang);//item.getLabelForShow(lang);
				JSONObject jsob = new JSONObject();
				jsob.put("name", item_name);
				jsob.put("ds_type", item_type);
				jsob.put("datasetId", item_id);
//				jsob.put("gid", groupID);
				jsob.put("gid",ugid);
				jsob.put("gname", ugname);//gname);
				result.add(jsob);
			}
			response.getWriter().print(result);
		} else if (option.equals("gmem")){
			String str = URLDecoder.decode(json, "utf8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String ds_type = (String) regtJSON.get("etype");
			String idStr = (String) regtJSON.get("idStr");
			int datasetId = Integer.parseInt((String) regtJSON.get("eid"));
			String lang = (String) regtJSON.get("lang");
			
			JSONArray result = new JSONArray();
			if(datasetId > 0 ){//if is edit
				ArrayList<URIMember> members = DatasetBrowsing.getDatasetMembers(ds_type, datasetId, lang);
				for(URIMember entry : members){
					JSONObject jsob = new JSONObject();
					jsob.put("uriID", entry.getURIID());
					jsob.put("label", entry.getLabel());//===========
					jsob.put("uri", entry.getURI());
					result.add(jsob);
				}
			}
			if(datasetId<=0){//if is create
				if(ds_type.contentEquals(DatasetItem.TEMP_GENRE)){//if is create from temp dataset
					String[] strList = idStr.split("_");
					ArrayList<Integer> docs = new ArrayList<Integer>();
					for(String id :strList){
						if(id.trim().length()>0){
							docs.add(Integer.parseInt(id));
						}
					}
					for(Integer uid :docs ){
						//HashSet<DatasetItem> items = DatasetBrowsing.getRelatedMygroupDataset(sds_type, sdatasetId, groupID);//===
						ArrayList<URIMember> members = DatasetBrowsing.getDatasetMembers("uri", uid, lang);
						for(URIMember entry : members){
							JSONObject jsob = new JSONObject();
							jsob.put("uriID", entry.getURIID());
							jsob.put("label", entry.getLabel());
							jsob.put("uri", entry.getURI());
							result.add(jsob);
						}
					}
				}else{
					String sds_type = (String) regtJSON.get("s_etype");
					int sdatasetId = Integer.parseInt((String) regtJSON.get("s_eid"));
//					int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
					HashSet<Integer> set = new HashSet<Integer>();
					set.addAll(DatasetBrowsing.getMembersByDatasetId(sdatasetId,sds_type));
//					if(sds_type.contentEquals("uri")){
//						set.add(sdatasetId);
//					}else if(sds_type.contentEquals("custom")){
//						set.addAll(DatasetBrowsing.getMembersByDatasetId(sdatasetId));
//					}
					for(Integer uid :set ){
						//HashSet<DatasetItem> items = DatasetBrowsing.getRelatedMygroupDataset(sds_type, sdatasetId, groupID);//===
						ArrayList<URIMember> members = DatasetBrowsing.getDatasetMembers("uri", uid, lang);
						for(URIMember entry : members){
							JSONObject jsob = new JSONObject();
							jsob.put("uriID", entry.getURIID());
							jsob.put("label", entry.getLabel());
							jsob.put("uri", entry.getURI());
							result.add(jsob);
						}
					}
				}
			}
			response.getWriter().print(result);
//		} else if (option.contentEquals("checkname")){
//			String str = URLDecoder.decode(json,"utf-8");
//			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
//			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
//			String name = (String) regtJSON.get("name");
//			String ds_type = (String) regtJSON.get("etype");
//			int ds_id = Integer.parseInt((String) regtJSON.get("eid"));
//			String userName =(String)regtJSON.get("userName");
//			int userID = -1;
//			try {
//				userID = UserNameIDCache.getInstance().getUserID(userName);
//			} catch (Throwable e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//			boolean getAuthority = Collaboration.getInstance().getEditAuthority(Collaboration.DATASET, ds_id, userID);
//
//			if (getAuthority) {
//				String status = Dataset.STATUS_CONFLICT;
//				if (ds_id > 0) {
////					if (CustomDataset.getNameByCustomDatasetID(ds_id) == null) {//=====
//					if (CustomDataset.getNameByCustomDatasetID(ds_id).size()<=0) {
//						status = Dataset.STATUS_REMOVE;
//					} else {
//						{
//							int entityID = CustomDataset.getCustomDatasetIdByNameGroup(name, groupID);
//							if (entityID <= 0) {
//								status = Dataset.STATUS_UPDATE;
//							} else if (entityID != ds_id) {
//								status = Dataset.STATUS_CONFLICT;
//							} else {
//								status = Dataset.STATUS_UPDATE;
//							}
//						}
//					}
//				} else {
//					int entityID = CustomDataset.getCustomDatasetIdByNameGroup(name, groupID);
//					if (entityID > 0)
//						status = Dataset.STATUS_CONFLICT;
//					else
//						status = "ok";
//				}
//
//				if (status.equals(STATUS_CONFLICT)
//						|| status.equals(STATUS_REMOVE)) {
//					Collaboration.getInstance().recoverAuthority(
//							Collaboration.DATASET, ds_id);
//				}
//				response.getWriter().print(status);
//
//			} else {
//				response.getWriter().print("noauthority");
//			}
			
			
//			String str = URLDecoder.decode(json, "utf8");
//			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
////			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
//			String ugname = (String)request.getSession().getAttribute("groupName");
//			if(ugname == null){
//				System.out.println("in checkname, no groupName session!");
//				JSONObject jsob = new JSONObject();
//				jsob.put("err", "no gsession");
//				response.getWriter().print(jsob);
//				return;				
//			}
//			
//			int ugid = Group.getGroupIdByName(ugname);
//			String name = (String) regtJSON.get("name");
////			String etype = (String) regtJSON.get("etype");
//			int eid = Integer.parseInt((String) regtJSON.get("eid"));
//			boolean exist  =false;
//			int entityID = CustomDataset.getCustomDatasetIdByNameGroup(name, ugid);//groupID);//Entity.getEntityIdByNameGroup(name, groupID);
//			if(entityID>=0){
//				if(entityID!=eid)
//					exist = true;
//			}
//			if(exist){
////				response.getWriter().print("exist");
//				response.getWriter().print("conflict");
//			}else {
//				response.getWriter().print("ok");
//			}
		} else if (option.contentEquals("checkmemURI")){//======?????
			String str = URLDecoder.decode(json, "utf8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
//			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
			String link = (String)regtJSON.get("link");
			String lang = (String)regtJSON.get("lang");
			
			int linkID = -1;
			try {
				linkID = URIIDCache.getInstance().getURIID(link, true);
			} catch (Throwable e) {
				e.printStackTrace();
			}
			
//			JSONArray array = new  JSONArray();
//			HashSet<DatasetItem> entityArray = DatasetBrowsing.getRelatedMygroupDataset("uri", linkID, groupID);//EntityManagement.getInstance().getRelatedMyGroupEntity(Entity.URI_ENTITY, linkID, groupID, null);
//			for(DatasetItem item:entityArray){
//				JSONObject jsob  =new JSONObject();
//				jsob.put("etype", item.getGenre());
//				jsob.put("eid", item.getId());
//				jsob.put("name", item.getName());
//				array.add(jsob);
//			}
			
			JSONObject result = new JSONObject();
			result.put("linkID", linkID);
			ArrayList<URIMember> member= DatasetBrowsing.getDatasetMembers("uri", linkID, lang);//EntityManagement.getInstance().getEntityMembers(Entity.URI_ENTITY, linkID, lang) ;
			result.put("label", member.get(0).getLabel());
			result.put("uri", member.get(0).getURI());
			
//			DocumentDataset dd = DocumentDataset.getInstance(linkID);
//			int contid = dd.getContributorId();
//			if(contid>0){
//				JSONObject contjsob = new JSONObject();
//				contjsob.put("id", contid);
//				contjsob.put("name", DatasetItem.getShowNameByDatasetID(linkID, DatasetItem.URI_GENRE, lang));
//				try {
//					contjsob.put("uri", URIIDCache.getInstance().getURI(contid));
//				} catch (Throwable e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				result.put("contributor", contjsob);
//			}
//			String date = dd.getDate();
//			if(date!=null && date.trim().length()>0){
//				result.put("date", date);
//			}
//			result.put("entity", array);
			response.getWriter().print(result);
//		}else if (option.equals("checkmemID")) {//=====????
//			String str = URLDecoder.decode(json, "utf8");
//			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
//			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
//			String lang = (String)regtJSON.get("lang");
//			int linkID =  Integer.parseInt((String) regtJSON.get("uriID"));
//			
//			JSONArray array = new  JSONArray();
//			HashSet<DatasetItem> entityArray = DatasetBrowsing.getRelatedMygroupDataset("uri", linkID, groupID);//EntityManagement.getInstance().getRelatedMyGroupEntity(Entity.URI_ENTITY, linkID, groupID, null);
//			for(DatasetItem item:entityArray){
//				JSONObject jsob  =new JSONObject();
//				jsob.put("etype", item.getGenre());
//				jsob.put("eid", item.getId());
//				jsob.put("name", item.getName());
//				array.add(jsob);
//			}
//			
//			JSONObject result = new JSONObject();
//			result.put("linkID", linkID);
//			ArrayList<URIMember> member= DatasetBrowsing.getDatasetMembers("uri", linkID, lang);//EntityManagement.getInstance().getEntityMembers(Entity.URI_ENTITY, linkID, lang) ;
//			result.put("label", member.get(0).getLabel());
//			result.put("uri", member.get(0).getURI());
//			result.put("entity", array);
//			response.getWriter().print(result);
		} else if (option.equals("recommendEntity")) {
			String str = URLDecoder.decode(json, "utf8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String ds_type = (String) regtJSON.get("etype");
			System.out.println("recommendEntity   ds_type:"+ds_type);
			int eid = Integer.parseInt((String) regtJSON.get("eid"));
//			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
			String lang = (String) regtJSON.get("lang");

			ArrayList<RecomdDatasetItem> recommends = DatasetBrowsing.getRecommendDataset(ds_type, eid);//,lang);//(ds_type, eid, groupID,lang);
			JSONArray result = new JSONArray();
			for (RecomdDatasetItem item : recommends) {
				int uriID = item.getURIID();
				String uri = null;
				try {
					uri = URIIDCache.getInstance().getURI(uriID);
				} catch (Throwable e1) {
					e1.printStackTrace();
				}
				String label = item.getLabel(lang) ;
				ArrayList<Entry<Integer, String>> typeArrayList =  URIResource.getTypesById(uriID, lang);
				ArrayList<Integer> originsArrayList = item.getOrigin();
				ArrayList<String> originLabelList = new ArrayList<String>();
				ArrayList<String> originURIList = new ArrayList<String>();
                for(Integer originID:originsArrayList){
                	String uriString = null;
					try {
						uriString = URIIDCache.getInstance().getURI(originID);
					} catch (Throwable e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                	String labelString = URIResource.getLabel(originID, lang);
                	originLabelList.add(labelString);
                	originURIList.add(uriString);
                }
                
                ArrayList<Boolean> cooccurArrayList = item.getCooccurMap();//.getSameasMap();
                ArrayList<ArrayList<Integer>> groupArrayList = item.getGroupScoreMap();
                
				JSONObject jsob = new JSONObject();
				jsob.put("uriID", uriID);
				jsob.put("label", label);
				jsob.put("uri", uri);
				
			    JSONArray typeArray = new JSONArray();
			    for(Entry<Integer, String> entry:typeArrayList){
			    	JSONObject typeObject = new JSONObject();
			    	typeObject.put("id", entry.getKey());
			    	typeObject.put("label", entry.getValue()) ;
			    	typeArray.add(typeObject);
			    }
			    
			    JSONArray originJsonArray = new JSONArray();
			    JSONArray sameJsonArray = new JSONArray();
			    JSONArray groupJsonArray = new JSONArray(); 
			    for(int i=0;i<originsArrayList.size();i++){
			    	int originID = originsArrayList.get(i);
			    	JSONObject object = new JSONObject();
			    	object.put("id", originID);
			    	object.put("uri", originURIList.get(i)) ;
			    	object.put("label", originLabelList.get(i)) ;
			    	originJsonArray.add(object);
			    	
			    	sameJsonArray.add(cooccurArrayList.get(i)) ;
			    	int comNum = groupArrayList.get(i).get(0) ;
			    	int entityNum  =  groupArrayList.get(i).get(1) ;
			    	JSONObject social = new JSONObject();
			    	social.put("numerator", comNum);
			    	social.put("denominator", entityNum);
			    	groupJsonArray.add(social);
			    }
			    jsob.put("origin",originJsonArray);
			    jsob.put("sameas",sameJsonArray);
			    jsob.put("social",groupJsonArray);
			    
				result.add(jsob);
			}
			
			response.getWriter().print(result);
//		}else if (option.equals("mergeEntity")) {
//			String str = URLDecoder.decode(json, "utf8");
//			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
//			String ds_type = (String) regtJSON.get("etype");
//			int datasetId = Integer.parseInt((String) regtJSON.get("eid"));
//			String lang = (String) regtJSON.get("lang");
//			
//		    ArrayList<URIMember> members = DatasetBrowsing.getDatasetMembers(ds_type, datasetId, lang); //EntityManagement.getInstance().getEntityMembers(etype, eid, lang);
//			
//			JSONArray result = new JSONArray();
//			for(URIMember member:members){
//				JSONObject jsob = new JSONObject();
//				jsob.put("id", member.getURIID()) ;
//				jsob.put("label", member.getLabel());
//				jsob.put("uri", member.getURI());
//				result.add(jsob);
//			}
//			
//			Entity.delete(ds_type, datasetId) ;
//			response.getWriter().print(result);
		} else if (option.contentEquals("svdataset")) {
			String str = URLDecoder.decode(json,"utf8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
//			int ugid = Group.getGroupIdByName(ugname);
			String name = (String) regtJSON.get("name");
			String ds_type = (String) regtJSON.get("etype");
			String comment = (String) regtJSON.get("comment");
			System.out.println("svdataset   ds_type:"+ds_type);
			int datasetId = Integer.parseInt((String) regtJSON.get("eid"));
			String lang =(String) regtJSON.get("lang");
			JSONArray array = (JSONArray)regtJSON.get("member");
//			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
//			String ugname = (String) request.getSession().getAttribute("groupName");
//			if(ugname == null){
//				System.out.println("in svdataset, no groupName session!");
//				JSONObject jsob = new JSONObject();
//				jsob.put("err", "no gsession");
//				response.getWriter().print(jsob);
//				return;				
//			}

			String userName =(String)regtJSON.get("user");
			int userID = -1;
			try {
				userID = UserNameIDCache.getInstance().getUserID(userName);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			int groupID = Integer.parseInt((String) regtJSON.get("groupID"));
			String groupName = null;
			try {
				groupName = GroupNameIDCache.getInstance().getName(groupID);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			JSONObject result = new JSONObject();
			boolean getAuthority = Collaboration.getInstance().getEditAuthority(Collaboration.DATASET, datasetId, userID);
			if(getAuthority){
				//同步锁
				Object groupLock = Collaboration.getInstance().getGroupProductLock(Collaboration.DATASET, groupID);
				//判断datasetId标识的自定义dataset是否因协同编辑而在别处已被删除
				boolean isthere = true;
				if(datasetId>0){
					synchronized (groupLock) {
						isthere = CustomDataset.isThere(datasetId);
					}
				}
				if(!isthere){
					Collaboration.getInstance().recoverAuthority(Collaboration.DATASET, groupID);
					result.put("status", this.STATUS_REMOVE);
				}else{
					//name和mem的存储
					int initialeid = datasetId;
					boolean exist = false;
					synchronized (groupLock) {
						HashSet<String> members = new HashSet<String>();
						for(int i=0;i<array.size();i++){
							int id = Integer.parseInt((String)array.get(i)) ;
							String uriStr = null;
							try {
								uriStr = URIIDCache.getInstance().getURI(id);
							} catch (Throwable e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
//							URIResource resource = ResourceFactory.getInstance().createURIResource(uriStr);
//							members.add(resource);
							members.add(uriStr);
						}
						
						//判断name是否重名
						if(datasetId>0){
							int existId = CustomDataset.getCustomDatasetIdByNameGroupLang(name, groupID, lang);
							if(existId>0 && existId != datasetId){
								exist = true;
							}
							// 在existId>0情况下，名字不冲突，赶紧存储名字及其成员
							// 其它entity就不能使用相同名字了和相关成员了(同组)
							if (!exist) {
								CustomDataset.addOrUpdateLangName(datasetId, name, lang);
								CustomDataset.updateMembers(datasetId, members);
							}
						}else{
							int existId = CustomDataset.getCustomDatasetIdByNameGroupLang(name, groupID,lang);
							if(existId>0){
								exist = true;
							}
							if(!exist){
								CustomDataset cdataset = new CustomDataset(name, groupName, lang, members);//(userName, groupName, lang, members);
								cdataset.serializeNew();
								System.out.println("serializeNew:"+datasetId+"|"+cdataset.getID());
								datasetId = cdataset.getID();
//								datasetId = CustomDataset.getCustomDatasetIdByNameGroupLang(name, groupID, lang);//其实也可以从cdataset对象里取
							}
						}
						
					}
					if(!exist){
						//description存储
						if(comment != null){
							DatasetComment.insert(ds_type, datasetId, groupName, lang, comment);
						}
						//用户visit日志
						DatasetItem.storeUserVisit(userID, ds_type, datasetId, lang);
						//收藏夹bookmark
						boolean bmed = DatasetBookmark.hasBookmarkedOnItemLanguage(userID, datasetId, ds_type, lang);
						if(!bmed){
							DatasetBookmark.insert(name, userName, ds_type, datasetId, lang);
						}
						//软件制品解锁
						Collaboration.getInstance().recoverAuthority(Collaboration.DATASET, initialeid);
						
						result.put("status", this.STATUS_OK);
						result.put("ds_id", datasetId);
						result.put("ds_type", DatasetItem.CUSTOM_GENRE);
					}else{
						Collaboration.getInstance().recoverAuthority(Collaboration.DATASET, datasetId);
						result.put("status", this.STATUS_CONFLICT);
					}
				}
			}else{
				// 没有编辑权限，返回noauthority
				result.put("status", this.STATUS_NOAUTHORITY);
			}
			// 最终返回结果
			response.getWriter().print(result);
		
		} else if (option.contentEquals("hasRDFDesc")) {
			String str = URLDecoder.decode(json,"utf-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String uri = (String) regtJSON.get("uri");
			String lang =(String) regtJSON.get("lang");
			
			boolean flag = true;
			if (uri == null || uri.trim().contentEquals("")) {
				flag = false;
			} else {
				uri = uri.trim();
			
				flag = EntityBrowsing.hasRDFDescription(uri);
			}

			if (!flag) {
				/* start:由于DBpedia等网站会被封掉，因此使用falcons数据集作为后备 */
				flag = EntityBrowsing.getReserveRDFDescription(uri);
				/* end:由于DBpedia等网站会被封掉，因此使用falcons数据集作为后备 */
			}
			
			JSONObject jsob = new JSONObject();
			if(!flag){
				jsob.put("flag", "invalid");
			}else{
				jsob.put("flag", "valid");
				int uriID = 0;
				try {
					uriID = URIIDCache.getInstance().getURIID(uri, false);
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String label = "";
				try {
					label = URILabelCache.getInstance().getLabel(uriID, lang);
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(label.trim().equals("")){
					label = URIUtil.getLocalname(uri);
				}
				
				jsob.put("uriID", uriID);
				jsob.put("label", label);
				jsob.put("uri", uri);
				
				DocumentDataset docds = DocumentDataset.getInstance(uriID);
				try {
					int conid = docds.getContributorId();
					if(conid>0){
						String conuri = URIIDCache.getInstance().getURI(conid);
						String contributor = DatasetItem.getShowNameByDatasetID(conid, DatasetItem.URI_GENRE, lang);//URILabelCache.getInstance().getLabel(conid, lang);
						if(contributor.trim().length()>0){
						    jsob.put("contributor", contributor);
						    jsob.put("conUri", conuri);
						}
					}
					String date = docds.getDate();
					if(date.trim().length()>0){
						jsob.put("date", date);
					}
					String desc = DatasetComment.getUriDescription(uriID, lang);
					if(desc.trim().length()>0){
						jsob.put("desc", desc);
					}
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			    
				
//			    JSONArray typeArray = new JSONArray();
//			    ArrayList<Entry<Integer,String>> typeArrayList = URIResource.getTypesById(uriID, lang);
//			    for(Entry<Integer, String> entry:typeArrayList){
//			    	JSONObject typeObject = new JSONObject();
//			    	typeObject.put("id", entry.getKey());
//			    	typeObject.put("label", entry.getValue()) ;
//			    	typeArray.add(typeObject);
//			    }
//			    jsob.put("type", typeArray) ;
				
			}
			response.getWriter().print(jsob);
		} else if(option.contentEquals("indexSearchDataset")){
			String str = URLDecoder.decode(json,"utf8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String kw = (String) regtJSON.get("kw");
			int start = ((Long) regtJSON.get("start")).intValue();
			int length = ((Long) regtJSON.get("length")).intValue();
			String lang =(String) regtJSON.get("lang");
			
			ArrayList<DatasetItem> searchDatasets = new ArrayList<DatasetItem>();
			int totalNum = DatasetBrowsing.kw2DatasetsForDsBuild(kw, start, length, lang, searchDatasets);
			System.out.println("inservlet:"+totalNum+"|"+searchDatasets.size());
			JSONArray array = new JSONArray();
			for(DatasetItem di : searchDatasets){
				JSONObject jsob = new JSONObject();
				jsob.put("ds_id", di.getId());
				jsob.put("ds_type", di.getGenre());
				String name = DatasetItem.getShowNameByDatasetID(di, lang);
				jsob.put("label", SearchIndexManager.getHighlightedText(name, kw));
				String uri = di.getUri();
				jsob.put("uri", uri);
				String uri_html = SearchIndexManager.getHighlightedText(uri, kw);
				jsob.put("uri_html", uri_html);
				String desc = null;
				if(di.getGenre().contentEquals(DatasetItem.CUSTOM_GENRE)){
					desc = DatasetComment.getCustomDescription(di.getId(), lang);
				}else{
					desc = DatasetComment.getUriDescription(di.getId(), lang);
				}
				if(desc != null && desc.length()>0){
					jsob.put("desc", SearchIndexManager.getHighlightedText(desc, kw));
				}
				array.add(jsob);
			}
//			System.out.println("array:"+array.toJSONString());
			JSONObject result = new JSONObject();
			result.put("items", array);
			result.put("totalNum", totalNum);
			response.getWriter().print(result);
		}
	}
}
